package com.zlict.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.zlict.admin.domain.RdFixedAssets;
import com.zlict.admin.service.RdFixedAssetsService;
import com.zlict.common.annotation.Log;
import com.zlict.common.core.controller.BaseController;
import com.zlict.common.core.domain.AjaxResult;
import com.zlict.common.core.domain.entity.SysUser;
import com.zlict.common.core.domain.model.LoginUser;
import com.zlict.common.core.page.TableDataInfo;
import com.zlict.common.enums.BusinessType;
import com.zlict.common.utils.SecurityUtils;
import com.zlict.common.utils.poi.ExcelUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 固定资产管理
 *
 * @author lqh
 * &#064;date  2025-06-09
 */
@RestController
@RequestMapping("/assets/fixedAsset")
@RequiredArgsConstructor
public class RdFixedAssetsController extends BaseController {


    private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory.getLogger(RdFixedAssetsController.class);

    private final RdFixedAssetsService rdFixedAssetsService;

    /**
     * 查询固定资产列表（分页）
     */
//    @PreAuthorize("@ss.hasPermi('admin:fixedAssets:getFixedAssetsList')")
    @GetMapping("/getFixedAssetsList")
    public TableDataInfo list(RdFixedAssets rdFixedAssets)
    {
        startPage();
        List<RdFixedAssets> list = rdFixedAssetsService.getFixedAssetsList(rdFixedAssets);
        return getDataTable(list);
    }

    /**
     * 固定资产新增
     */
    //    @PreAuthorize("@ss.hasPermi('admin:fixedAssets:addFixedAsset')")
    @PostMapping("addFixedAsset")
    public AjaxResult addFixedAsset(@Validated @RequestBody RdFixedAssets rdFixedAssets) {

       return toAjax(rdFixedAssetsService.insertRdFixedAssets(rdFixedAssets));
    }

    /**
     * 固定资产修改
     */
    //    @PreAuthorize("@ss.hasPermi('admin:fixedAssets:updateFixedAsset')")
    @PostMapping("updateFixedAsset")
    public AjaxResult updateFixedAsset(@Validated @RequestBody RdFixedAssets rdFixedAssets) {
        return toAjax(rdFixedAssetsService.updateRdFixedAssets(rdFixedAssets));
    }

    /**
     * 固定资产列表导出
     */

//    @PreAuthorize("@ss.hasPermi('admin:fixedAssets:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response,  @RequestBody RdFixedAssets rdFixedAssets)
    {
        List<RdFixedAssets> list = rdFixedAssetsService.getFixedAssetsList(rdFixedAssets);
        ExcelUtil<RdFixedAssets> util = new ExcelUtil<>(RdFixedAssets.class);
        util.exportExcel(response, list, "固定资产数据");
    }

    /**
     * 固定资产导入（仪器/设备）
     */
//    @PreAuthorize("@ss.hasPermi('assets:fixedAssets:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<RdFixedAssets> util = new ExcelUtil<>(RdFixedAssets.class);
        List<RdFixedAssets> rdFixedAssetsList = util.importExcel(file.getInputStream());
//        Long operName = getUserId();
        Long  operName = 1L;
        Map<String, Object> map = rdFixedAssetsService.importRdFixedAssets(rdFixedAssetsList, updateSupport, operName);
        return success(map);
    }


    /**
     * 固定资产导入（建筑物）
     */
    //    @PreAuthorize("@ss.hasPermi('assets:fixedAssets:importBuildingData')")
    @PostMapping("/importBuildingData")
    public AjaxResult importBuildingData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<RdFixedAssets> util = new ExcelUtil<>(RdFixedAssets.class);
        List<RdFixedAssets> rdFixedAssetsList = util.importExcel(file.getInputStream());
        //        Long operName = getUserId();
        Long  operName = 1L;
        Map<String, Object> map= rdFixedAssetsService.importRdFixedAssetsBuilding(rdFixedAssetsList, updateSupport, operName);
        return success(map);
    }


    /**
     * 导入模板下载
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<RdFixedAssets> util = new ExcelUtil<>(RdFixedAssets.class);
        util.importTemplateExcel(response, "设备数据");
    }

    /**
     * 删除固定资产
     */
//    @PreAuthorize("@ss.hasPermi('assets:intangibleassets:remove')")
    @Log(title = "删除固定资产", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(rdFixedAssetsService.deleteRdFixedAssetsByIds(ids));
    }


    /**
     * 查询固定资产列表(不分页)
     */
//    @PreAuthorize("@ss.hasPermi('admin:fixedAssets:getList')")
    @GetMapping("/getList")
    public AjaxResult getList(RdFixedAssets rdFixedAssets)
    {
        List<RdFixedAssets> list = rdFixedAssetsService.getFixedAssetsList(rdFixedAssets);
        return success(list);
    }

    /**
     * 固定资产折旧/租赁明细
     */
    @GetMapping("/getDepreciationList")
    public AjaxResult getDepreciationList(Long id){
        return success(rdFixedAssetsService.selectRdAssetsCostDetailById(id));
    }

    /**
     * 批量启用
     */
    @PostMapping("/batchEnable")
    public AjaxResult batchEnable(@RequestBody Long[] ids) {
        return toAjax(rdFixedAssetsService.batchEnable(ids));
    }
    /**
     * 批量禁用
     */
    @PostMapping("/batchDisable")
    public AjaxResult batchDisable(@RequestBody Long[] ids) {
        return toAjax(rdFixedAssetsService.batchDisable(ids));
    }
}
